//---------------------------------------------------------------------------- Package
const _ = require('lodash')
//---------------------------------------------------------------------------- Config
const { MYSQL } = require('../../config')
//---------------------------------------------------------------------------- Plugin
const mysql = require('../../plugin/util/mysql')
const sheet = require('../../plugin/util/sheet')
//---------------------------------------------------------------------------- GET
exports.get = {
  /**
   * 区域列表
   */
  '/area/list': async (ctx, next) => {
    let area = await mysql.query(MYSQL.XIONGAN, ['select * from area'], [null])
    sheet[0].data = area[0]
    ctx.body = sheet[0]
  },
  /**
   * 首页轮播列表
   */
  '/swiper/list': async (ctx, next) => {
    let swiper = await mysql.query(MYSQL.XIONGAN, ['select * from swiper order by sort'], [null])
    sheet[0].data = swiper[0]
    ctx.body = sheet[0]
  },
  /**
   * 登录
   */
  '/user/login/:username/:password': async (ctx, next) => {
    let username = ctx.params.username
    let password = ctx.params.password
    let data = await mysql.query(MYSQL.XIONGAN, ['select * from admin where username=? and password = ?'], [username,password])
    if(data[0].length > 0){
      sheet[0].data = data
      ctx.body = sheet[0]
    }else{
      // console.log(data);
      // sheet[1004].data = data
      // sheet[1004].massage = '账号或密码错误'
      ctx.body = sheet[1004]
    }
  },
   	/**
	 * 获取一级岗位类别
	 */
	'/bigcat/list/one': async (ctx, next) => {
		let data = await mysql.query(MYSQL.XIONGAN, ['select * from job_type where bid=0'], null)
		sheet[0].data = data[0]
		ctx.body = sheet[0]
	},
  	/**
	 * 获取二级岗位类别
	 */
	'/smallcat/list/:mid': async (ctx, next) => {
		let data = await mysql.query(MYSQL.XIONGAN, ['select * from job_type where bid=? and mid = 0'], [ctx.params.mid])
		sheet[0].data = data[0]
		ctx.body = sheet[0]
  },
    /**
   * 系统消息列表
   */
  '/swiper/msg_list': async (ctx, next) => {
    let limit = ' limit 0,10'
    if(ctx.query.page > 0){
			limit = ' limit ' + (ctx.query.page-1)*10 + ',10'
		}    
    let msg = await mysql.query(MYSQL.XIONGAN, ['select * from personmsg ' + limit], [null])
    sheet[0].data = msg
    ctx.body = sheet[0]
  },
    /**
   * 欢迎消息列表
   */
  '/swiper/msg_list_1': async (ctx, next) => {
    let limit = ' limit 0,10'
    if(ctx.query.page > 0){
			limit = ' limit ' + (ctx.query.page-1)*10 + ',10'
		}    
    let msg = await mysql.query(MYSQL.XIONGAN, ['select * from usermsg where type=1' + limit], [null])
    sheet[0].data = msg
    ctx.body = sheet[0]
  },
}
// ---------------------------------------------------------------------------- POST
exports.post = {
   /**
   * 添加区域
   */
  '/add/area': async (ctx, next) => {
    let { name } =ctx.request.fields
    let data=await mysql.query(MYSQL.XIONGAN, ['insert into area (name) values (?) '], [name])
    sheet[0].message='添加成功'
    ctx.body = sheet[0]
  },
  /**
   * 上传图片
   */
  '/upload': async (ctx, next) => {
    let data = await $.upload(ctx)
    sheet[0].data = data
    ctx.body = sheet[0]
  },
/**
   * 添加系统消息
   */
  '/add/msg': async (ctx, next) => {
    let { msg, type } =ctx.request.fields
    let time = _.now().toString().substr(0,10)
    await mysql.query(MYSQL.XIONGAN, ['insert into usermsg (msg,type,time) values (?,?,?) '], [msg,type,time])
    sheet[0].message='添加成功'
    ctx.body = sheet[0]
  },
   /**
   * 添加轮播
   */
  '/swiper/add': async (ctx, next) => {
    let { img, url, sort, type, lid } = ctx.request.fields
    img[0] = "xiongan/swiper/"+img[0]
    let live = await mysql.query(MYSQL.XIONGAN,['select * from live where id=?'],[lid])
    let sql=[]
    let arg=[]
    if(type == 1){
      sql = ['insert into swiper (sort, img, url, type, lid) values(?,?,?,?,?)']
      arg = [sort,live[0][0].img,live[0][0].href,1,lid]
      await mysql.query(MYSQL.XIONGAN, sql, arg)
      await mysql.query(MYSQL.XIONGAN,['update live set ishome=1 where id=?'],[lid])
      sheet[0].message = '添加成功'
      ctx.body = sheet[0]
    }else{
      sql = ['insert into swiper ( sort, img, url, type ) values(?,?,?,?)']
      arg = [sort,img,url,type]
      await mysql.query(MYSQL.XIONGAN, sql, arg)
      sheet[0].message='添加成功'
      ctx.body = sheet[0]
    }
    
  },
  /**
 * 添加岗位类别
 */
'/product/add': async (ctx, next) => {
    let { name, bid, mid,} = ctx.request.fields
    let sql = ['insert into job_type (name, bid, mid) values( ?, ?, ?)']
    let arg = [name, bid, mid]
    let product = await mysql.query(MYSQL.XIONGAN, sql, arg)
    sheet[0].data = product[0]
    ctx.body = sheet[0]
  },
}
// ---------------------------------------------------------------------------- PUT
exports.put = {
   /**
   * 编辑区域
   */
  '/edit/area': async (ctx, next) => {
    if (!_.isArray(ctx.request.fields)) {
			ctx.request.fields = [ctx.request.fields]
		}
    let sql = []
    let arg = []
		for (let item of ctx.request.fields) {
      let { name, id } = item
      sql.push('update area set name=? where id =?')
      arg.push([ name, id ])
		}
    await mysql.query(MYSQL.XIONGAN, sql, arg)
    sheet[0].message='更新成功'
    ctx.body = sheet[0]
  },
  /**
   * 编辑轮播
   */
  '/edit/swiper': async (ctx, next) => {
    if (!_.isArray(ctx.request.fields)) {
			ctx.request.fields = [ctx.request.fields]
		}
    let sql = []
    let arg = []
		for (let item of ctx.request.fields) {
      let { sort, url, id } = item
      sql.push('update swiper set sort=?, url=? where id =?')
      arg.push([ sort, url, id ])
		}
    await mysql.query(MYSQL.XIONGAN, sql, arg)
    sheet[0].message='更新成功'
    ctx.body = sheet[0]
  },
  
}
// ---------------------------------------------------------------------------- DELETE
exports.delete = {
  /**
   * 删除轮播
   */
  '/swiper/remove/:ids': async (ctx, next) => {
    let ids = ctx.params.ids
    ids = ids.replace(/[|]/g, ',')
    let sql = ['delete from swiper where id in (' + ids + ')']
    let arg = null
    await mysql.query(MYSQL.XIONGAN, sql, arg)
    sheet[0].message='删除成功'
    ctx.body = sheet[0]
  },
    /**
   * 删除系统消息
   */
  '/usermsg/remove/:id': async (ctx, next) => {
    let id = ctx.params.id
    id = id.replace(/[|]/g, ',')
    let sql = ['delete from usermsg where id in (' + id + ')']
    let arg = null
    await mysql.query(MYSQL.XIONGAN, sql, arg)
    sheet[0].message='删除成功'
    ctx.body = sheet[0]
  }
}